module.exports = (
  /**
   * @deprecated
   * @type {Record<string,any>}
   */
  options,
  /**
   * @type {import('thinkjs').Application}
   */
  app
) => {
  return (
    /**
     * @type {import('koa').Context}
     */
    ctx,
    /**
     * @type {import('koa').Next}
     */
    next
  ) => {
    if (ctx.request.method !== 'WEBSOCKET') {
      const origin = ctx.request.headers.origin;

      if (origin) {
        // 添加跨域
        ctx.set('Access-Control-Allow-Origin', origin);
        ctx.set('Access-Control-Allow-Credentials', 'true');
        ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
        ctx.set('Access-Control-Allow-Headers', 'Content-Type, Authorization');
      }
    }

    return next();
  };
};
